package com.mushuilingfeng.ddd.user.infrastructure.verifycode;

import com.mushuilingfeng.ddd.user.domain.base.VerifyCodeType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 能处理各种验证码
 *
 * @author
 * @date 2024/9/29
 */
@Slf4j
@Service
public class DefaultVerifyCodeService extends AbstractVerifyCodeService {
    private final ConcurrentHashMap<String, String> db = new ConcurrentHashMap<>();

    @Override
    public boolean canProcess(VerifyCodeType type) {
        return true;
    }

    @Override
    void sendVerifyCode(String tel) {
        Random rd = new Random(System.currentTimeMillis());
        String code = "" + rd.nextInt(100000, 999999);
        log.info("DefaultVerifyCodeService生成验证码，code = {}", code);
        db.put(tel, code);
    }

    @Override
    boolean verifyCode(String tel, String code) {
        log.info("DefaultVerifyCodeService开始验证，tel = {}，code = {}", tel, code);
        String relCode = db.get(tel);
        return relCode != null && relCode.equals(code);
    }

}
